﻿#region MIT
/*
Engine Of Evermore (http://code.google.com/p/engineofevermore/)
Copyright (c) 2008, 2009 Michael Woerister

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using EngineOfEvermore.Common;
using System.Diagnostics;

namespace EngineOfEvermore.Imaging
{
    public abstract class ImageMapFrame : IDisposable
    {
        #region Properties
        public bool IsEmpty { get; private set; }
        public Rectangle OriginalArea { get; private set; }
        public Rectangle OriginalNonTransparentArea { get; private set; }
        public Rectangle ConvertedArea { get; private set; }
        public Point Offset { get; private set; }
        public Vector2d RelativeSize { get; private set; }
        public Vector2d RelativeOffset { get; private set; }
        public RectangleF RelativeArea { get; private set; }

        public int Width { get { return this.OriginalArea.Width; } }
        public int Height { get { return this.OriginalArea.Height; } }
        #endregion

        #region Constructors
        protected ImageMapFrame( bool isEmpty, Rectangle originalArea,
            Rectangle originalNonTransparentArea, Rectangle convertedArea )
        {
            this.IsEmpty = isEmpty;
            this.OriginalArea = originalArea;
            this.OriginalNonTransparentArea = originalNonTransparentArea;
            this.ConvertedArea = convertedArea;

            this.Offset = new Point( originalNonTransparentArea.X - originalArea.X,
                                     originalNonTransparentArea.Y - originalArea.Y );

            this.RelativeSize = new Vector2d( convertedArea.Width / (float) originalArea.Width,
                                              convertedArea.Height / (float) originalArea.Height );

            this.RelativeOffset = new Vector2d( this.Offset.X / (float) originalArea.Width,
                                                this.Offset.Y / (float) originalArea.Height );

            this.RelativeArea = new RectangleF( this.RelativeOffset.X, this.RelativeOffset.Y,
                                                this.RelativeSize.X, this.RelativeSize.Y );
        }

        ~ImageMapFrame()
        {
            _dispose( false );
        }
        #endregion

        #region Abstract Drawing Methods

        public abstract void DrawFrame( Vector2d position, Vector2d scale, Color color, 
            bool flipX, bool flipY );

        public abstract void DrawFrameInRectangle( RectangleF position, Color color, bool flipX,
            bool flipY );

        public abstract void DrawFrameRotated( Vector2d center, float rotation,
            Vector2d scale, Color color, bool flipX, bool flipY );

        public abstract void DrawFrameClippedAndRotated( Vector2d center, float rotation, 
            Vector2d scale, bool flipX, bool flipY, Color color, RectangleF clipArea );
        #endregion

        #region IDisposable Members
        public void Dispose()
        {
            _dispose( true );
            GC.SuppressFinalize( this );
        }

        protected abstract void _dispose( bool disposing );
        #endregion
    }
}
